package com.indexingsystem.db.mongoDB.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.indexingsystem.boss.entity.AuditLogs;
import com.indexingsystem.boss.entity.DhResourceauditlogs;
import com.indexingsystem.boss.entity.DhTodayStorageInfos;
import com.indexingsystem.boss.entity.Pusers;
import com.indexingsystem.boss.entity.Resources;
import com.indexingsystem.boss.entity.RoleGroups;
import com.indexingsystem.boss.entity.Roles;
import com.indexingsystem.boss.entity.SpecialResourceDatas;
import com.indexingsystem.boss.entity.UserInfos;
import com.indexingsystem.boss.entity.UserInstitution;
import com.indexingsystem.boss.entity.UserRole;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.boss.utils.JsonUtils;
import com.indexingsystem.db.mongoDB.MongoManager;
import com.indexingsystem.db.mongoDB.comon.MongoDBUtils;
import com.indexingsystem.db.mongoDB.comon.MongoTableName;
import com.indexingsystem.db.mongoDB.po.UserPublishMongoVo;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;

public class UserPublishMongo {
	/**
	 * 平台用户管理mongo新增单条数据.
	 * 
	 * @param articlesResourceMongoVo
	 * @throws Exception
	 */
	public static void addUser(UserPublishMongoVo userPublishMongoVo) throws Exception {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_USER_LIST + "添加 ------->" + com.alibaba.fastjson.JSON.toJSONString( userPublishMongoVo, true ) );
		if (null != userPublishMongoVo) {
			Document mainDocument = new Document();
			mainDocument.put( "_id", userPublishMongoVo.getUserId() );
			mainDocument.put( "userId", userPublishMongoVo.getUserId() );

			// 用户基本信息
			Document pusersDoc = MongoDBUtils.transform2Doc( userPublishMongoVo.getPusers() );
			mainDocument.put( "pusers", pusersDoc );

			// 用户详情基本信息
			Document userInfosDoc = MongoDBUtils.transform2Doc( userPublishMongoVo.getUserInfos() );
			mainDocument.put( "userInfos", userInfosDoc );

			// 用户角色列表信息
			List<Document> userRoleListDocList = new ArrayList<Document>();
			if (null != userPublishMongoVo.getUserRoleList() && userPublishMongoVo.getUserRoleList().size() > 0) {
				for (UserRole userRole : userPublishMongoVo.getUserRoleList()) {
					Document userRoleDoc = MongoDBUtils.transform2Doc( userRole );
					userRoleListDocList.add( userRoleDoc );
				}
			}
			mainDocument.put( "userRoleList", userRoleListDocList );

			// 角色列表信息
			List<Document> roleListDocList = new ArrayList<Document>();
			if (null != userPublishMongoVo.getRolesList() && userPublishMongoVo.getRolesList().size() > 0) {
				for (Roles roles : userPublishMongoVo.getRolesList()) {
					Document roleDoc = MongoDBUtils.transform2Doc( roles );
					roleListDocList.add( roleDoc );
				}
			}
			mainDocument.put( "rolesList", roleListDocList );

			// 角色组列表信息
			List<Document> roleGroupsDocList = new ArrayList<Document>();
			if (null != userPublishMongoVo.getRoleGroupsList() && userPublishMongoVo.getRoleGroupsList().size() > 0) {
				for (RoleGroups roleGroups : userPublishMongoVo.getRoleGroupsList()) {
					Document roleGroupsDoc = MongoDBUtils.transform2Doc( roleGroups );
					roleGroupsDocList.add( roleGroupsDoc );
				}
			}
			mainDocument.put( "roleGroupsList", roleGroupsDocList );

			// 用户机构信息
			Document userInstitutionDoc = MongoDBUtils.transform2Doc( userPublishMongoVo.getUserInstitution() );
			mainDocument.put( "userInstitution", userInstitutionDoc );
			manager.add( mainDocument, MongoTableName.DH_USER_LIST );
		}
	}

	/**
	 * 平台用户管理修改用户信息资源mongo修改.
	 * 
	 * @param articlesResourceMongoVo
	 * @throws Exception
	 */
	public static void updateUser(UserPublishMongoVo userPublishMongoVo) throws Exception {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_USER_LIST + "修改 ------->" + com.alibaba.fastjson.JSON.toJSONString( userPublishMongoVo, true ) );
		if (null != userPublishMongoVo) {
			Document mainDocument = new Document();
			mainDocument.put( "userId", userPublishMongoVo.getUserId() );

			// 用户基本信息
			Document pusersDoc = MongoDBUtils.transform2Doc( userPublishMongoVo.getPusers() );
			mainDocument.put( "pusers", pusersDoc );

			// 用户详情基本信息
			Document userInfosDoc = MongoDBUtils.transform2Doc( userPublishMongoVo.getUserInfos() );
			mainDocument.put( "userInfos", userInfosDoc );

			// 用户角色列表信息
			List<Document> userRoleListDocList = new ArrayList<Document>();
			if (null != userPublishMongoVo.getUserRoleList() && userPublishMongoVo.getUserRoleList().size() > 0) {
				for (UserRole userRole : userPublishMongoVo.getUserRoleList()) {
					Document userRoleDoc = MongoDBUtils.transform2Doc( userRole );
					userRoleListDocList.add( userRoleDoc );
				}
			}
			mainDocument.put( "userRoleList", userRoleListDocList );

			// 角色列表信息
			List<Document> roleListDocList = new ArrayList<Document>();
			if (null != userPublishMongoVo.getRolesList() && userPublishMongoVo.getRolesList().size() > 0) {
				for (Roles roles : userPublishMongoVo.getRolesList()) {
					Document roleDoc = MongoDBUtils.transform2Doc( roles );
					roleListDocList.add( roleDoc );
				}
			}
			mainDocument.put( "rolesList", roleListDocList );

			// 角色组列表信息
			List<Document> roleGroupsDocList = new ArrayList<Document>();
			if (null != userPublishMongoVo.getRoleGroupsList() && userPublishMongoVo.getRoleGroupsList().size() > 0) {
				for (RoleGroups roleGroups : userPublishMongoVo.getRoleGroupsList()) {
					Document roleGroupsDoc = MongoDBUtils.transform2Doc( roleGroups );
					roleGroupsDocList.add( roleGroupsDoc );
				}
			}
			mainDocument.put( "roleGroupsList", roleGroupsDocList );

			// 用户机构信息
			Document userInstitutionDoc = MongoDBUtils.transform2Doc( userPublishMongoVo.getUserInstitution() );
			mainDocument.put( "userInstitution", userInstitutionDoc );

			UpdateOptions options = new UpdateOptions().upsert( true );
			Bson bson = Filters.and( Filters.eq( "userId", userPublishMongoVo.getUserId() ) );
			Document doc = new Document( "$set", mainDocument );
			manager.updateOne( bson, doc, options, MongoTableName.DH_USER_LIST );
		}
	}

	/**
	 * 批量删除用户信息更新mongodb.
	 * 
	 * @param map
	 * @return
	 */
	public static void deleteUserByIds(List<String> userIdList) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.in( "userId", userIdList ) );
		manager.deleteAll( bson, MongoTableName.DH_USER_LIST );
	}

	/**
	 * 通过userId从mongo缓存中获取用户缓存信息.
	 * 
	 * @param userId
	 * @param connName
	 * @return
	 */
	public static UserPublishMongoVo selectUserInfoByUserId(String userId) throws Exception {
		MongoManager manager = new MongoManager();
		UserPublishMongoVo userPublishMongoVo = null;
		if (StringUtils.isNotBlank( userId )) {
			Bson bson = Filters.eq( "userId", userId );
			LogCvt.info( MongoTableName.DH_USER_LIST + "通过userId获取用户信息查询条件:" + Arrays.asList( bson ).toString() );
			List<Document> documentsList = manager.queryAggregateList( bson, null, MongoTableName.DH_USER_LIST );
			if (null != documentsList && documentsList.size() > 0) {
				userPublishMongoVo = new UserPublishMongoVo();
				for (Document document : documentsList) {
					Pusers pusers = new Pusers();
					pusers = (Pusers) MongoDBUtils.setFieldValue( pusers, Document.parse( JsonUtils.toJson( document.get( "pusers" ) ) ) );
					if (null != pusers) {
						userPublishMongoVo.setPusers( pusers );
					}

					UserInfos userInfos = new UserInfos();
					userInfos = (UserInfos) MongoDBUtils.setFieldValue( userInfos, Document.parse( JsonUtils.toJson( document.get( "userInfos" ) ) ) );
					if (null != userInfos) {
						userPublishMongoVo.setUserInfos( userInfos );
					}

					UserInstitution userInstitution = new UserInstitution();
					userInstitution = (UserInstitution) MongoDBUtils.setFieldValue( userInstitution,
							Document.parse( JsonUtils.toJson( document.get( "userInstitution" ) ) ) );
					if (null != userInfos) {
						userPublishMongoVo.setUserInstitution( userInstitution );
					}

					List<UserRole> userRoleList = new ArrayList<UserRole>();
					userRoleList = JSONArray.parseArray( JsonUtils.toJson( document.get( "userRoleList" ) ), UserRole.class );
					if (null != userRoleList && userRoleList.size() > 0) {
						userPublishMongoVo.setUserRoleList( userRoleList );
					}

					List<Roles> rolesList = new ArrayList<Roles>();
					rolesList = JSONArray.parseArray( JSON.toJSONStringWithDateFormat( document.get( "rolesList" ), "yyyy-MM-dd HH:mm:ss.SSS" ),
							Roles.class );
					if (null != rolesList && rolesList.size() > 0) {
						userPublishMongoVo.setRolesList( rolesList );
					}

					List<RoleGroups> roleGroupsList = new ArrayList<RoleGroups>();
					roleGroupsList = JSONArray.parseArray(
							JSON.toJSONStringWithDateFormat( document.get( "roleGroupsList" ), "yyyy-MM-dd HH:mm:ss.SSS" ), RoleGroups.class );
					if (null != roleGroupsList && roleGroupsList.size() > 0) {
						userPublishMongoVo.setRoleGroupsList( roleGroupsList );
					}
				}
			}
		}
		return userPublishMongoVo;
	}
}